package com.admin.thread;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.admin.bean.AdminSpBean;
import com.admin.bean.AdminSpCpBean;
import com.admin.dao.AdminSpCpDao;
import com.admin.service.AdminSpService;
import com.admin.util.BaseUtil;
import com.admin.util.BillingUtil;
import com.admin.util.SqlUtil;
import com.admin.util.db.DbOperation;

public class DayIvrStatThread extends Thread {

	public static long SLEEP_TIME = 1000*10;
	
	private boolean started = true;
	
	private AdminSpCpDao adminSpCpDao;	
	private AdminSpService adminSpDao;
	
	public DayIvrStatThread(AdminSpCpDao adminSpCpDao,AdminSpService adminSpDao) {
		this.adminSpCpDao = adminSpCpDao;
		this.adminSpDao = adminSpDao;
	}
	
	
	public boolean isStarted() {
		return started;
	}

	public void setStarted(boolean started) {
		this.started = started;
	}

	@Override
	public void run() {
		try {
			Thread.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
			return;
		}
		
		while(started){
			try {
				Thread.sleep(SLEEP_TIME);
			} catch (InterruptedException e) {
				return;
			}
			task();
			try{
				if(started){
					boolean flag = false;
					Date date = new Date();
					if(date.getHours()==1) {
					//if(true){
						System.out.println("start everyday stat task..");
						try {
							task();
						} catch (Exception e) {
							e.printStackTrace();
							try {
								Thread.sleep(60*1000*60);
							} catch (Exception e1) {
								e1.printStackTrace();
								return;
							} finally {

							}
							continue;
						} finally {

						}
						
						flag = true;
					} else {
						flag = false;
						System.out.println("sleep 1 hours");
						try {
							Thread.sleep(60*1000*60);
						} catch (Exception e) {
							// TODO: handle exception
							e.printStackTrace();
							return;
						} finally {

						}
					}
				}
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}

	public void task(){
		try {
			System.out.println("执行IVR每天统计");
			List<AdminSpCpBean> spcpList = adminSpCpDao.findAll();
			
			for (AdminSpCpBean adminSpCpBean : spcpList) {
				
				AdminSpBean adminSpBean = adminSpDao.findAdminSpById(adminSpCpBean.getSpid());
				
				if(adminSpBean!=null&&adminSpBean.getBussiness().equals("IVR")) {
					String[] provinces = adminSpBean.getSpprovince().split(",");
					List<Integer> provinceIds = new ArrayList<Integer>();
					for (String string : provinces) {
						int provinceId = BaseUtil.stringToInt(string);
						provinceIds.add(new Integer(provinceId));
					}
					provinceIds.add(new Integer(0));
					for (Integer integer : provinceIds) {
						//用户数
						StringBuilder sbmoUser = new StringBuilder("select count(distinct mobile) from halt where province = ");
						sbmoUser.append(integer);
						sbmoUser.append(" and callno = '");
						sbmoUser.append(adminSpCpBean.getSpcode());
						sbmoUser.append("' and halttime >= Convert(datetime, (CONVERT(varchar(10),DateAdd(day,-1,getDate()),120)+' 00:00:00')) and halttime < Convert(datetime, (CONVERT(varchar(10),DateAdd(day,0,getDate()),120)+' 00:00:00')) ");
						
						//System.out.println("mo:"+sbmo.toString());
						String cpmoUserSql = sbmoUser.toString()+" and ischeck = 0";
						
						
						int moUser = SqlUtil.getIntResult(sbmoUser.toString(), DbOperation.DB_MUZHI);
						int cpmoUser = SqlUtil.getIntResult(cpmoUserSql, DbOperation.DB_MUZHI);
						
						
						//次数
						StringBuilder sbmo = new StringBuilder("select count(0) from halt where province = ");
						sbmo.append(integer);
						sbmo.append(" and callno = '");
						sbmo.append(adminSpCpBean.getSpcode());
						sbmo.append("' and halttime >= Convert(datetime, (CONVERT(varchar(10),DateAdd(day,-1,getDate()),120)+' 00:00:00')) and halttime < Convert(datetime, (CONVERT(varchar(10),DateAdd(day,0,getDate()),120)+' 00:00:00')) ");
						
						//System.out.println("mo:"+sbmo.toString());
						String cpmoSql = sbmo.toString()+" and ischeck = 0";
						
						int mo = SqlUtil.getIntResult(sbmo.toString(), DbOperation.DB_MUZHI);
						int cpmo = SqlUtil.getIntResult(cpmoSql, DbOperation.DB_MUZHI);
						
						
						//时长
						StringBuilder sbmt = new StringBuilder("select sum(convert(int,feeseconds))  from halt where province = ");
						sbmt.append(integer);
						sbmt.append(" and createdtime = '");
						sbmt.append(adminSpCpBean.getSpcode());
						sbmt.append("' and halttime >= Convert(datetime, (CONVERT(varchar(10),DateAdd(day,-1,getDate()),120)+' 00:00:00')) and halttime < Convert(datetime, (CONVERT(varchar(10),DateAdd(day,0,getDate()),120)+' 00:00:00')) ");
						
						//System.out.println("mt:"+sbmt.toString());
						String cpmtSql = sbmt.toString()+" and ischeck = 0";
						int mt = SqlUtil.getIntResult(sbmt.toString(),DbOperation.DB_MUZHI);
						int cpmt = SqlUtil.getIntResult(cpmtSql, DbOperation.DB_MUZHI);
						
						
						//收入
						StringBuilder sbstatmt = new StringBuilder("select sum(fee) from halt where province = ");
						sbstatmt.append(integer);
						sbstatmt.append(" and createdtime = '");
						sbstatmt.append(adminSpCpBean.getSpcode());
						sbstatmt.append("' and halttime >= Convert(datetime, (CONVERT(varchar(10),DateAdd(day,-1,getDate()),120)+' 00:00:00')) and halttime < Convert(datetime, (CONVERT(varchar(10),DateAdd(day,0,getDate()),120)+' 00:00:00')) ");
						
						
						//System.out.println("statmt:"+sbstatmt.toString());
						String cpstatmtSql = sbstatmt.toString()+" and ischeck = 0";
						int statmt = SqlUtil.getIntResult(sbmt.toString(),DbOperation.DB_MUZHI);
						int cpstatmt = SqlUtil.getIntResult(cpstatmtSql,DbOperation.DB_MUZHI);
						if(mo>0||mt>0||statmt>0) {
							StringBuilder statsql = new StringBuilder("insert into T_ivr_stat_fee(dt,gatewayid,cityid,channel,calluser,callnum,calltime,statfee,cpcalluser,cpcallnum,cpcalltime,cpstatfee,province)  values(");
							statsql.append("CONVERT(varchar(10),DateAdd(day,-1,getDate()),120),");
							statsql.append(adminSpCpBean.getSpid());
							statsql.append(",");
							statsql.append(integer);
							statsql.append(",");
							statsql.append(adminSpCpBean.getCpid());
							statsql.append(",");
							statsql.append(moUser);
							statsql.append(",");
							statsql.append(mo);
							statsql.append(",");
							statsql.append(mt);
							statsql.append(",");
							statsql.append(statmt);
							statsql.append(",");
							
							statsql.append(cpmoUser);
							statsql.append(",");
							statsql.append(cpmo);
							statsql.append(",");
							statsql.append(cpmt);
							statsql.append(",");
							statsql.append(",");
							statsql.append(cpstatmt);
							statsql.append(",");
							statsql.append(integer);
							statsql.append(")");
							SqlUtil.executeUpdate(statsql.toString(), DbOperation.DB_MUZHI);
						}
					}
				}
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		} finally {

		}
	}
	
	
	
}
